package com.jiayou.im.controller

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.jiayou.im.entity.Chat
import com.jiayou.im.service.impl.ChatServiceImpl
import com.jiayou.im.vo.Resoult
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

/**
 *
 *
 * 前端控制器
 *
 *
 * @author lishuang
 * @since 2020-11-20
 */
@RestController
@RequestMapping("/im/chat")
class ChatController {

    @Autowired
    private lateinit var chatServiceImpl: ChatServiceImpl


    @RequestMapping("getMessage")
    fun getMessage(@RequestParam("id1") id1: String, @RequestParam("id2") id2: String): Resoult<List<Chat>> {
        val list = chatServiceImpl.list(
            KtQueryWrapper<Chat>(Chat::class.java)
                .and {
                    it.eq(Chat::id1, id1).eq(Chat::id2, id2)
                }.or {
                    it.eq(Chat::id2, id1).eq(Chat::id1, id2)
                }

                .orderByDesc(Chat::createTime).last("limit 20")
        )
        return Resoult(200, list)
    }

}
